package com.zyy.study.sort;

import java.util.Arrays;

public class InsertSort {

    public static void main(String[] args) {
        int[] array = {2, 3, 5, 2, 1};
        sort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void sort(int[] array) {
        int length = array.length;
        for (int i = 1; i < length; i++) {
            int tmp = array[i];
            int index = i;
            for (int j = i; j > 0; j--) {
                if (array[j - 1] > tmp) {
                    array[j] = array[j - 1];
                    index = j - 1;
                }
            }
            array[index] = tmp;
        }
    }
}
